package com.qf.dao.impl;

import com.qf.dao.EntryDao;
import com.qf.domain.EBookEntry;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 2020/12/9 9:52
 *
 * @author：Lixubiao
 */
public class EntryDaoImpl implements EntryDao {
    private QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public Long selecttoltal() {
        String sql="select count(*) from ebook_entry";
        try {
            return qr.query(sql, new ScalarHandler<>());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }

    }

    @Override
    public List<EBookEntry> selectEntries(Integer pageSize, Integer pageNum) {
        String sql="select * from ebook_entry order by id limit ?,?";
        try {
            return  qr.query(sql, new BeanListHandler<>(EBookEntry.class),(pageNum-1)*pageSize,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }
    }
}
